﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class admin_module_form_category : System.Web.UI.UserControl
{
    categoryBus catBus = new categoryBus();
    languageBus langbus = new languageBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLanguage();
            loadData();
        }
        
    }
    public void loadLanguage()
    {
        
        listlang.DataSource = langbus.getAllLanguage();
        listlang.DataBind();
    }
    public void loadData()
    {
        category cat = null;
        ArrayList lstlang = langbus.getArrayLanguage();
        int path = 0;
        if (Request.QueryString["path"]!="")
        {
            path= Convert.ToInt32(Request.QueryString["path"]);
            cat = catBus.getCategoryEntity(path);
        }
        DataTable dt = catBus.getCategoriesByLanguageID(1);
        listcat.Items.Add(new ListItem("Root", "0"));
        foreach (DataRow r in dt.Rows)
        {
              listcat.Items.Add(new ListItem(this.getNameByID(Convert.ToInt32(r["category_id"]), 1).ToString(),r["category_id"].ToString())); 
        }
        if (cat != null)
        {
            listcat.SelectedValue = cat.Parent_id.ToString();
            date_added.SelectedDate = Convert.ToDateTime(cat.Date_added);
        }
        else
        {
            date_added.SelectedDate = DateTime.Now;
        }
        
        DataTable content = this.ContentByLanguage();
        DataRow dr;
        foreach (language lang in lstlang)
        {
            dr = content.NewRow();
            category_des catdes=null;
            if (path != 0)
            {
                catdes = catBus.getCategoryDescription(path, Convert.ToInt32(lang.Language_id));
            }
            if (catdes == null)
            {
                dr["language_id"] = lang.Language_id.ToString();
                dr["category_id"] = "";
                dr["name"] = "";
                dr["meta_description"] = "";
            }
            else
            {
                dr["language_id"] = lang.Language_id.ToString();
                dr["category_id"] = catdes.Category_id.ToString();
                dr["name"] = catdes.Name;
                dr["meta_description"] =catdes.Meta_description;
            }
            content.Rows.Add(dr);
        }
        genaral.DataSource = content;
        genaral.DataBind();

    }

    public DataTable ContentByLanguage()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("language_id");
        dt.Columns.Add("category_id");
        dt.Columns.Add("name");
        dt.Columns.Add("meta_description");
        return dt;
    }


    protected void OK_Click(object sender, ImageClickEventArgs e)
    {
        int category_id = 0;
        int parent_id = Convert.ToInt32(listcat.SelectedValue);
        DateTime date_a = date_added.SelectedDate;
        if (Request.QueryString["path"]!= "" && Request.QueryString["path"]!=null)
        {
            category_id = Convert.ToInt32(Request.QueryString["path"]);
            catBus.editCategoryData(category_id, parent_id, true, date_a);
            
        }
        else
        {
            catBus.editCategoryData(0, parent_id, true, date_a);
            category_id =catBus.getLastCategoryID(0);
           
        }
        //Response.Write(genaral.Items.Count);
        foreach (RepeaterItem item in genaral.Items)
        {
            Label language_id = item.FindControl("language_id") as Label;
            TextBox name = item.FindControl("name") as TextBox;
            TextBox des = item.FindControl("des") as TextBox;           
            catBus.editcategory(category_id, Convert.ToInt32(language_id.Text), name.Text.ToString(), des.Text.ToString());
        }
        Response.Redirect("default.aspx?action=categories");
    }
    protected void Cancle_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("default.aspx?action=categories");
    }
    public string getNameByID(int category_id, int language_id)
    {
        string output = "";

        ArrayList arr = catBus.getCategoryByCategoryID(category_id, language_id);
        if (arr != null)
        {

            string parent = "";
            if (Convert.ToInt32(arr[1]) != 0)
            {
                parent = this.getNameByID(Convert.ToInt32(arr[1]), language_id);

            }
            if (parent != "")
            {
                output += parent + " >> ";
            }
            output += arr[0].ToString();
        }
        return output;

    }
}